﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Isis.Views;
using Isis.Interfaces;

namespace Isis.Views
{
    public class SubTabWriter
    {

        public string GetSubTabUrlFragment(string subTab)
        {
            return subTab.Replace(" ", "_").ToLower();
        }

        public string RenderSubTabs(string _basePath, string tabName, IEnumerable<string> subTabs, TextWriter writer, string currentSubTabName)
        {
            if (subTabs.Any())
            {
                var sb = new StringBuilder("<ul id=\"secondary\">");
                var firstSubTab = subTabs.FirstOrDefault();

                foreach (string subTab in subTabs)
                {
                    var subTabUrlFragment = GetSubTabUrlFragment(subTab);

                    if (subTabUrlFragment == currentSubTabName || 
                        (subTab == firstSubTab && String.IsNullOrEmpty(currentSubTabName))
                        )
                    {
                        sb.Append(string.Format("<li class=\"selected\">{0}</li>",
                            subTab));
                    }
                    else
                    {
                        sb.Append(string.Format("<li><a href=\"{0}/{1}/{2}\">{3}</a></li>",
                            _basePath,
                            tabName.ToLower(),
                            subTabUrlFragment,
                            subTab));
                    }
                }
                sb.Append("</ul>");
                return sb.ToString();


            }

            return currentSubTabName;
        }
    }
}
